package com.gohome.gohomechat.model;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;

import com.gohome.gohomechat.GoHomeModel;
import com.gohome.gohomechat.model.entify.CurrentUser;
import com.gohome.gohomechat.model.modellistener.EditorModelInterface;
import com.gohome.gohomechat.presenter.callback.EditorCallBack;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;

import java.io.ByteArrayOutputStream;

/**
 * Created by dllo on 16/3/9.
 */
public class EditorModel implements EditorModelInterface {
    @Override
    public void save(final CurrentUser user,final EditorCallBack editorCallBack) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("PersonalDataGoHome");
      //  Log.i("wcl", GoHomeModel.getInstance().getCurrentUserName() + " 登录用户 判断");
        query.whereEqualTo("username", GoHomeModel.getInstance().getCurrentUserName());
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (e == null) {

                    ParseQuery<ParseObject> query1 = ParseQuery.getQuery("PersonalDataGoHome");
                    query1.getInBackground(object.getObjectId(), new GetCallback<ParseObject>() {
                        @Override
                        public void done(ParseObject object, ParseException e) {
                            if (e == null) {
                                //  object.put("username", personal_username.getText().toString().trim());
                                object.put("sex", user.getSex());
                                object.put("zodiaz", user.getZodiaz());
                                object.put("boold", user.getBoold());
                                object.put("age", user.getAge());
                                object.put("singnature", user.getSingnature());
                                object.put("birthday", user.getBirthday());
                                object.put("nick_name", user.getNick());
                                object.put("email", user.getEmail());
//                                Bitmap bitmap = BitmapFactory.decodeFile(user.getHead());// decodeResource(getResources(), R.mipmap.ic_launcher);
//                                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                                bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
//                                byte[] bytes = byteArrayOutputStream.toByteArray();
                                if (GoHomeModel.getInstance().getHeadByte() != null) {
                                    ParseFile parseFile = new ParseFile(GoHomeModel.getInstance().getCurrentUserName() + "head.png", GoHomeModel.getInstance().getHeadByte());
                                    parseFile.saveInBackground();
                                    object.put("head_img", parseFile);
                                }
                                object.saveInBackground(new SaveCallback() {
                                    @Override
                                    public void done(ParseException e) {
                                        if (e == null) {
//                                            PersonalActivity.this.finish();
//                                            Toast.makeText(PersonalActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                            editorCallBack.success();
                                        } else
                                            editorCallBack.failed();
//                                            Log.i("wcl", e.getMessage() + "更改数据");
//                                        Toast.makeText(PersonalActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            } else {
                                Log.i("wcl", e.getMessage() + "  objectId");
                                editorCallBack.failed();
                            }
                        }
                    });
                } else {
                    Log.i("wcl", e.getMessage() + "  username");
                    editorCallBack.failed();
                }
            }
        });
    }
}
